####################################################################################
###
###   Immigration, Public Housing and Support for the French National Front
###   Gloria Gennaro
###
###   Paper Table A6
###
####################################################################################

library(estimatr)


################################################################################
# Set Up
################################################################################

# Load Working Directories
source(paste0(wd_main, '/2_code/00_working_directories.R'))

# Load data
data = read.csv(paste0(wd_data, "/FES2017_data.csv"), sep=";")


################################################################################
# Variable definitions
################################################################################

# Agglomeration
data$agglo = ifelse(data$A5>=5 & data$A5<=8, 1, 0)

# Outcomes
data$agree_imm_bad_econ = ifelse(data$O27 %in% c(2, 3), 1, 0)
data$agree_imm_bad_culture2 = ifelse(data$O28 <= 2, 1, 0)
data$agree_crime = ifelse(data$O29 <= 2, 1, 0)
data$agree_imm_welfare = ifelse(data$O30<=1, 1, 0)

# Voting
data$lepen = ifelse(data$V2==9 | data$V4==2, 1, 0)

# Demographics
data$male = ifelse(data$S2==0, 1, 0)
data$age = data$S4 
data$education = data$S7 
data$unemployed = ifelse(data$S9 %in% c(1, 3), 1, 0) 


################################################################################
# Estimations
################################################################################

results = function(mod){
  print(round(summary(mod)$coefficients[c('lepen', 'agglo', 'lepen:agglo'), 1:2], 3))
  print(paste0("Observations: ", mod$nobs))
  print(paste0("R-squared: ", round(mod$r.squared, 3)))
}

print('------------- Column 1 --------------')
mod = lm_robust(agree_imm_bad_econ ~ lepen*agglo, data=data) 
results(mod)

print('------------- Column 2 --------------')
mod = lm_robust(agree_imm_bad_econ ~ lepen*agglo + male + as.factor(age) + as.factor(education) + unemployed, data=data) 
results(mod)

print('------------- Column 3 --------------')
mod = lm_robust(agree_imm_bad_culture2 ~ lepen*agglo, data=data) 
results(mod)

print('------------- Column 4 --------------')
mod = lm_robust(agree_imm_bad_culture2 ~ lepen*agglo + male + as.factor(age) + as.factor(education) + unemployed, data=data) 
results(mod)

print('------------- Column 5 --------------')
mod = lm_robust(agree_crime ~ lepen*agglo, data=data) 
results(mod)

print('------------- Column 6 --------------')
mod = lm_robust(agree_crime ~ lepen*agglo + male + as.factor(age) + as.factor(education) + unemployed, data=data) 
results(mod)

print('------------- Column 7 --------------')
mod = lm_robust(agree_imm_welfare ~ lepen*agglo, data=data)
results(mod)

print('------------- Column 8 --------------')
mod = lm_robust(agree_imm_welfare ~ lepen*agglo + male + as.factor(age) + as.factor(education) + unemployed, data=data)
results(mod)

